<?php

# PMB Online
# Aplikasi PMB Online untuk Pendaftaran Calon Mahasiswa di PT/PTS Indonesia
# 
# version 1.0
# class xls
# file controllerts/xls.php
# created Feb 16, 2013 6:12:04 AM
# 
# (c)2012, arif.laksito@gmail.com

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Xls extends CI_Controller {

    var $data = array();

    public function __construct() {
        parent::__construct();

        // trace error
        // set true to see error log
        $this->output->enable_profiler(false);

        $this->load->model('authlogin');
        if (!$this->authlogin->valid_cookies()) {
            redirect('admin');
        } else {
            $this->data['sess'] = $this->authlogin->get_cookies();
        }
    }

    public function all() {
        $this->load->model(array('csiswa','jurusan'));
        $mhs = $this->csiswa->get_all();        
        
        $this->load->library('excel');
        $this->excel->setActiveSheetIndex(0);
        $this->excel->getActiveSheet()->setTitle('data siswa');

        $this->excel->getActiveSheet()->setCellValue('A1', 'Data Pendaftar STIMYKPN');
        $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);

        $field = array(65 =>'NODAF','NAMA LENGKAP','GELOMBANG','JURUSAN DITERIMA','JENIS PENDAFTARAN',
            'TEMPAT LAHIR','TGL LAHIR','JENIS KEL.','AGAMA','STATUS','ALAMAT ASAL','KEC. ASAL','KOTA/KAB ASAL',
            'PROV. ASAL','KODEPOS ASAL','ALAMAT TINGGAL','KEC. TINGGAL','KOTA/KAB TINGGAL','PROV. TINGGAL',
            'KODEPOS TINGGAL','TELP/HP','EMAIL','NAMA AYAH','NAMA IBU','ALAMAT ORTU','KEC. ORTU','KOTA/KAB ORTU',
            'PROV. ORTU','KODEPOS ORTU','TELP/HP ORTU','PEKERJAAN ORTU','SEKOLAH/PT ASAL','TH LULUS','RATA2 NEM/IPK',
            'JURUSAN','ALAMAT SEKOLAH/PT','KOTA/KAB SEKOLAH','PROV. SEKOLAH','KODEPOS SEKOLAH','NAMA KEP.SEKOLAH',
            'TGL DAFTAR','TGL UJIAN','INFORMASI','CS DAFTAR','CS EDIT','CS LULUS');
        
        foreach ($field as $k => $r) {   
            if ($k < 91) $c = chr($k);
            if ($k == 91) $c = 'AA';
            if ($k == 92) $c = 'AB';
            if ($k == 93) $c = 'AC';
            if ($k == 94) $c = 'AD';
            if ($k == 95) $c = 'AE';
            if ($k == 96) $c = 'AF';
            if ($k == 97) $c = 'AG';
            if ($k == 98) $c = 'AH';
            if ($k == 99) $c = 'AI';
            if ($k == 100) $c = 'AJ';
            if ($k == 101) $c = 'AK';
            if ($k == 102) $c = 'AL';
            if ($k == 103) $c = 'AM';
            if ($k == 104) $c = 'AN';
            if ($k == 105) $c = 'AO';
            if ($k == 106) $c = 'AP';
            if ($k == 107) $c = 'AQ';
            if ($k == 108) $c = 'AR';
            if ($k == 109) $c = 'AS';
            if ($k == 110) $c = 'AT';
            $this->excel->getActiveSheet()->setCellValue($c . '3', $r);
            $this->excel->getActiveSheet()->getStyle($c . '3')->getFont()->setBold(true);
        }
        
        $i = 4;
        foreach ($mhs as $m) {
            $jur = $this->jurusan->get_by_id($m['id_jurusan']);
            
            $this->excel->getActiveSheet()->setCellValue('A' . $i, $m['nodaf']);
            $this->excel->getActiveSheet()->setCellValue('B' . $i, $m['nama_lengkap']);
            $this->excel->getActiveSheet()->setCellValue('C' . $i, $m['nama_gel']);
            $this->excel->getActiveSheet()->setCellValue('D' . $i, ($m['id_jurusan']!='')?$jur['jenjang'].'-'.$jur['nama_jurusan']:"");
            $s = array('Reguler','Pindahan','CSR');
            $this->excel->getActiveSheet()->setCellValue('E' . $i, $s[$m['status_daftar']]);
            $this->excel->getActiveSheet()->setCellValue('F' . $i, $m['tempat_lahir']);
            $this->excel->getActiveSheet()->setCellValue('G' . $i, $m['tgl_lahir']);
            $this->excel->getActiveSheet()->setCellValue('H' . $i, ($m['jkel'])?"Laki-laki":"Perempuan");
            $a = $this->config->item('agama');
            $this->excel->getActiveSheet()->setCellValue('I' . $i, $a[$m['agama']]);
            $this->excel->getActiveSheet()->setCellValue('J' . $i, ($m['status_nikah']==1)?"Menikah":"Belum Menikah");
            $this->excel->getActiveSheet()->setCellValue('K' . $i, $m['alamat_asal']);
            $this->excel->getActiveSheet()->setCellValue('L' . $i, $m['kec_asal']);
            $this->excel->getActiveSheet()->setCellValue('M' . $i, $m['kota_asal']);
            $this->excel->getActiveSheet()->setCellValue('N' . $i, $m['prov_asal']);
            $this->excel->getActiveSheet()->setCellValue('O' . $i, $m['kodepos_asal']);
            $this->excel->getActiveSheet()->setCellValue('P' . $i, $m['alamat']);
            $this->excel->getActiveSheet()->setCellValue('Q' . $i, $m['kec']);
            $this->excel->getActiveSheet()->setCellValue('R' . $i, $m['kota']);
            $this->excel->getActiveSheet()->setCellValue('S' . $i, $m['prov']);
            $this->excel->getActiveSheet()->setCellValue('T' . $i, $m['kodepos']);
            $this->excel->getActiveSheet()->setCellValue('U' . $i, $m['telp']);
            $this->excel->getActiveSheet()->setCellValue('V' . $i, $m['email']);
            $this->excel->getActiveSheet()->setCellValue('W' . $i, $m['nama_ayah']);
            $this->excel->getActiveSheet()->setCellValue('X' . $i, $m['nama_ibu']);
            $this->excel->getActiveSheet()->setCellValue('Y' . $i, $m['alamat_ortu']);
            $this->excel->getActiveSheet()->setCellValue('Z' . $i, $m['kec_ortu']);
            $this->excel->getActiveSheet()->setCellValue('AA' . $i, $m['kota_ortu']);
            $this->excel->getActiveSheet()->setCellValue('AB' . $i, $m['prov_ortu']);
            $this->excel->getActiveSheet()->setCellValue('AC' . $i, $m['kodepos_ortu']);
            $this->excel->getActiveSheet()->setCellValue('AD' . $i, $m['telp_ortu']);
            $this->excel->getActiveSheet()->setCellValue('AE' . $i, $m['pekerjaan']);
            $this->excel->getActiveSheet()->setCellValue('AF' . $i, $m['sekolah_asal']);
            $this->excel->getActiveSheet()->setCellValue('AG' . $i, $m['th_lulus']);
            $this->excel->getActiveSheet()->setCellValue('AH' . $i, $m['nem']);
            $this->excel->getActiveSheet()->setCellValue('AI' . $i, $m['jurusan']);
            $this->excel->getActiveSheet()->setCellValue('AJ' . $i, $m['alamat_sekolah']);
            $this->excel->getActiveSheet()->setCellValue('AK' . $i, $m['kota_sekolah']);
            $this->excel->getActiveSheet()->setCellValue('AL' . $i, $m['prov_sekolah']);
            $this->excel->getActiveSheet()->setCellValue('AM' . $i, $m['kodepos_sekolah']);
            $this->excel->getActiveSheet()->setCellValue('AN' . $i, $m['nama_kep_sek']);
            $this->excel->getActiveSheet()->setCellValue('AO' . $i, $m['tgl_daftar']);
            $this->excel->getActiveSheet()->setCellValue('AP' . $i, $m['tgl_ujian']);
            $this->excel->getActiveSheet()->setCellValue('AQ' . $i, $m['info']);            
            $this->excel->getActiveSheet()->setCellValue('AR' . $i, $m['cs_daftar']);
            $this->excel->getActiveSheet()->setCellValue('AS' . $i, $m['cs_edit']);
            $this->excel->getActiveSheet()->setCellValue('AT' . $i, $m['cs_lulus']);
            
            $i++;
        }
        
        $filename = 'siswa_all.xls';
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');

        $objWriter = IOFactory::createWriter($this->excel, 'Excel5');
        $objWriter->save('php://output');
    }

}